import { defineStore } from 'pinia';
import pinia from '@/stores';
import type { UserState } from '@/types/user';
import { userLogin } from '@/api/user';
import { md5 } from 'js-md5';
export const useUserStoreHook = defineStore('userInfo', {
    state: (): UserState => ({
        username: '',
        accessToken: '',
        refreshToken: '',
        roles: [],
        factoryId: '0'
    }),
    getters: {},
    actions: {
        async storeUserLogin(username: string, password: string) {
            return new Promise(async (resolve, reject) => {
                const { data: res } = await userLogin(username, md5(password));
                this.factoryId = res.factory_id;
                this.accessToken = res.access_token;
                this.refreshToken = res.refresh_token;
                resolve(res);
            });
        }
    },
    persist: {
        key: 'userInfo',
        storage: localStorage,
        pick: ['accessToken', 'refreshToken']
    }
});

export function useUserStore() {
    return useUserStoreHook(pinia);
}
